package git.bds.nyc.admin.service.audit.gx.authentication;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import git.bds.nyc.admin.convert.AuditConvert;
import git.bds.nyc.admin.model.vo.AuditFarmerAuthenticationInfoVO;
import git.bds.nyc.admin.model.vo.AuditFarmerAuthenticationVO;
import git.bds.nyc.enums.AuditType;
import git.bds.nyc.enums.RoleType;
import git.bds.nyc.exception.BusinessException;
import git.bds.nyc.page.PageParam;
import git.bds.nyc.page.PageResult;
import git.bds.nyc.result.ResultCode;
import git.bds.nyc.role.domain.SysUserRole;
import git.bds.nyc.role.domain.authentication.FarmerAuthentication;
import git.bds.nyc.role.mapper.mp.SysUserRoleMapper;
import git.bds.nyc.role.mapper.mp.authentication.FarmerAuthenticationMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 成大事
 * @since 2023/6/9 14:49
 */
@Service
public class AuditFarmerAuthenticationServiceImpl implements AuditFarmerAuthenticationService{

    @Resource
    private FarmerAuthenticationMapper farmerAuthenticationMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 按类型获取农民页面
     *
     * @param pageParam 页面参数
     * @param type      类型
     * @return {@link PageResult}<{@link AuditFarmerAuthenticationVO}>
     */
    @Override
    public PageResult<AuditFarmerAuthenticationVO> getFarmerPageByType(PageParam pageParam, int type) {
        Page<FarmerAuthentication> page = farmerAuthenticationMapper.selectPage(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()),
                new MPJLambdaWrapper<FarmerAuthentication>()
                        .select(FarmerAuthentication::getId, FarmerAuthentication::getRealName, FarmerAuthentication::getGender, FarmerAuthentication::getIdCardNum)
                        .eq(FarmerAuthentication::getAuditStatus, type)
                        .orderByDesc(FarmerAuthentication::getCreateTime));
        List<AuditFarmerAuthenticationVO> list = AuditConvert.INSTANCE.toAuditFarmerAuthenticationVO(page.getRecords());
        return new PageResult<>(list,page.getTotal());
    }

    /**
     * 通过id获取农民身份验证信息
     *
     * @param id 身份证
     * @return {@link AuditFarmerAuthenticationInfoVO}
     */
    @Override
    public AuditFarmerAuthenticationInfoVO getFarmerAuthenticationInfoById(Long id) {
        FarmerAuthentication one = farmerAuthenticationMapper.selectOne(new LambdaQueryWrapper<FarmerAuthentication>()
                .select(FarmerAuthentication.class, i -> !i.getColumn().equals(FarmerAuthentication.USER_ID)
                        && !i.getColumn().equals(FarmerAuthentication.UPDATE_TIME)
                ).eq(FarmerAuthentication::getId, id));
        if(one == null){
            throw new BusinessException(ResultCode.NOT_FOUND.getCode(), ResultCode.NOT_FOUND.getMessage());
        }
        return AuditConvert.INSTANCE.toAuditFarmerAuthenticationInfoVO(one);
    }

    /**
     * 用id审计农民身份验证
     *
     * @param id     身份证
     * @param status 地位
     * @return {@link Boolean}
     */
    @Override
    public Boolean auditFarmerAuthenticationById(Long id, int status) {
        FarmerAuthentication one = farmerAuthenticationMapper.selectOne(new LambdaQueryWrapper<FarmerAuthentication>().select(FarmerAuthentication::getUserId).eq(FarmerAuthentication::getId, id));
        if(one == null){
            throw new BusinessException(ResultCode.NOT_FOUND.getCode(), ResultCode.NOT_FOUND.getMessage());
        }
        if(status == AuditType.PASS.getValue()){
            sysUserRoleMapper.update(null,new LambdaUpdateWrapper<SysUserRole>()
                    .set(SysUserRole::getRoleId, RoleType.FARMER.getValue())
                    .eq(SysUserRole::getUserId,one.getUserId()));

        }
        return farmerAuthenticationMapper.update(null,new LambdaUpdateWrapper<FarmerAuthentication>()
                .set(FarmerAuthentication::getAuditStatus,status)
                .eq(FarmerAuthentication::getId,id)) > 0;
    }
}
